#Rem
Header: APP TIMING. 
#End

Import mojo
Import agfx

#Rem
summary: global value for actual FPS count
#End
Global AppTime_UPS

Global AppTime_Iterator
#Rem
summary:  APP current time
#End
Global AppTime_CurrentTime
Global AppTime_PauseStart=0
Global AppTime_Speed:Float=1.0
Global AppTime_DesiredLoopTime:Float=Float(1000.0/TARGET_FPS)
Global AppTime_LastUpdateTime=0
Global AppTime_LastUPSTime
Global AppTime_DesiredFrequency%
#Rem
summary:  counter for seconds
#End
Global AppTime_Seconds:Int
Global AppTime_OffsetSeconds:Int

Global fpsCount:Int=0
Global startTime:Int = Millisecs()

#Rem
summary:  called every OnUpdate()
[i]only for internal use[/i]
#End
Function UpdateAppTime() 

		AppTime_CurrentTime = Millisecs()
		AppTime_LastUPSTime = AppTime_CurrentTime - AppTime_LastUpdateTime
		AppTime_Speed = AppTime_LastUPSTime / (1000.0 / TARGET_FPS)
		AppTime_LastUpdateTime = AppTime_CurrentTime

		If Millisecs() - startTime >= 1000
			AppTime_UPS = fpsCount
			fpsCount = 0
			startTime = Millisecs()
		Else
			fpsCount+=1
		End

End Function


#Rem
summary:  return actual time in milisecs.
#End
Function AppTime:Float()
	Return AppTime_CurrentTime
End 

#Rem
summary:  return seconds count from start
#End
Function AppSecTime:Int()
	Return AppTime_Seconds
End 

#Rem
summary:  reset seconds counter
#End
Function AppResetSecTime:Void()
	AppTime_OffsetSeconds = Millisecs()
End 

#Rem
summary:  return actual delta-time
#End
Function AppSpeed:Float()
	Return AppTime_Speed
End 

#Rem
summary:  return actual frame per seconds
#End
Function FPS:Int()
	Return AppTime_UPS
End 

#Rem
summary:  stop incement seconds
#End
Function PauseApp:Void()
	If AppTime_PauseStart Then Return
	AppTime_PauseStart=Millisecs()
        AppTime_UPS = 0
        AppTime_Speed = 0
End

#Rem
summary:  resume application timing
#End
Function ResumeApp:Void()
	If Not AppTime_PauseStart Return
	If AppTime_LastUpdateTime Then
		Local elapsed=Millisecs()-AppTime_PauseStart
		AppTime_LastUpdateTime += elapsed
	EndIf
	AppTime_PauseStart=0
    UpdateAppTime()
End